package cn.tedu.controller;

import cn.tedu.dao.UserDao;
import cn.tedu.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "RegServlet",urlPatterns = "/reg")
public class RegServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        //获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repassword = request.getParameter("repassword");
        if(!username.matches("^[1][3,4,5,7,8][0-9]{9}$") || "".equals(password.trim()) || "".equals(repassword.trim())){
            System.out.println("格式不正确或输入框不能为空");
            response.sendRedirect("/register.html");
            return;
        }
        if(password.equals(repassword)){
            UserDao dao = new UserDao();
            Boolean b = dao.reg(username, password);
            if (b) {
                System.out.println("注册成功!");
                //重定向到首页列表页面
                response.sendRedirect("/login");
            } else {
                System.out.println("注册失败!");
                //重新定向到注册页面
                response.sendRedirect("/register.html");
            }
        }else {
            System.out.println("两次密码不一致");
            //重新定向到注册页面
            response.sendRedirect("/register.html");
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
